package com.thememaker.lockstyle.control;

import com.jfoenix.controls.JFXListView;
import com.jfoenix.svg.SVGGlyph;
import com.thememake.common.Context;
import com.thememake.ui.R;
import com.thememake.ui.Toast;
import com.thememaker.lockstyle.bean.ui.BaseView;
import com.thememaker.lockstyle.parsii.ExpressionUtils;
import com.thememaker.util.SVGLoadUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Consumer;

import static com.thememaker.lockstyle.LockstyleMainController.buttonColor;

public class CreateControlDialog {
    private Stage stage;
    private HBox hBox;
    private Path lockscreenPath;
    private ExpressionUtils expressionUtils;
    private Consumer<BaseView> baseViewConsumer;
    private ImageControl imageControl;

    public CreateControlDialog(final Stage stg, Path lockscreenPath, ExpressionUtils expressionUtils, Consumer<BaseView> baseViewConsumer) {
        this.lockscreenPath = lockscreenPath;
        this.expressionUtils = expressionUtils;
        this.baseViewConsumer = baseViewConsumer;
        stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.initOwner(stg);
        stage.setTitle("新建组件");
        stage.setResizable(false);
        stage.getIcons().add(new Image(
                getClass().getResourceAsStream(Context.getDrawable(R.drawable.launcher))));
        hBox = new HBox();
        hBox.getStylesheets().add("style/lockstylemain.css");
        initView();
        Scene scene = new Scene(hBox, 850, 700, Color.LIGHTGREEN);
        stage.setScene(scene);
    }


    private void initView() {
        VBox vBoxLeft = new VBox();
        vBoxLeft.setPrefWidth(220);
        vBoxLeft.setPrefHeight(750);
        vBoxLeft.setPadding(new Insets(10));
        vBoxLeft.setAlignment(Pos.TOP_CENTER);
        vBoxLeft.getChildren().add(getLeftListView());



        VBox rightVBox = new VBox();
        rightVBox.setPrefSize(600,700);
        rightVBox.setPadding(new Insets(15,0,0,0));
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setStyle("-fx-padding: 0;-fx-focus-traversable: false;");
        scrollPane.setPrefSize(630,625);
        imageControl = new ImageControl();
        scrollPane.setContent(imageControl.initLockscreenControl(null,lockscreenPath,expressionUtils,baseView -> {

        }));
        HBox hBoxRight = new HBox();
        Button buttonCancel = new Button("取消");
        buttonCancel.setPrefWidth(120);
        buttonCancel.setFont(Font.font(14));
        Button buttonOk = new Button("确定");
        buttonOk.setPrefWidth(120);
        buttonOk.setFont(Font.font(14));
        buttonCancel.setId("diy-button");
        buttonOk.setId("diy-button");
        hBoxRight.getChildren().addAll(buttonCancel,buttonOk);

        rightVBox.getChildren().add(scrollPane);
        rightVBox.getChildren().add(hBoxRight);
        hBoxRight.setAlignment(Pos.CENTER);
        hBoxRight.setSpacing(30);

        hBox.setSpacing(20);
        hBox.getChildren().addAll(vBoxLeft,rightVBox);

        initBtn(buttonCancel,buttonOk);

    }

    public static HBox getLeftContrlItem(String name,SVGGlyph stackPane){
        HBox hBoxLeft = new HBox();
        hBoxLeft.setPrefWidth(150);
        hBoxLeft.setPrefHeight(30);
        hBoxLeft.setPadding(new Insets(10, 10, 10, 10));
        hBoxLeft.setSpacing(15);
        hBoxLeft.setAlignment(Pos.CENTER_LEFT);
//        hBoxLeft.setStyle("-fx-background-color: white;-fx-background-radius: 5;-fx-border-radius: 5;-fx-effect: dropshadow( gaussian , #E8E8EA , 10,0.6,0,1 );");
        Label label = new Label(name);
        label.setId("diy-fontColor");
        label.setFont(Font.font(16));
//        hBoxLeft.getChildren().addAll(SVGLoadUtils.getInstance().getImageSVG(14,buttonColor),label);
//        hBoxLeft.getChildren().addAll(SVGLoadUtils.getInstance().getImageSVG(16,buttonColor),label);

        hBoxLeft.getChildren().addAll(stackPane,label);
        return hBoxLeft;
    }

    private JFXListView<HBox> getLeftListView(){
        JFXListView<HBox> leftList = new JFXListView<HBox>();
        leftList.setPrefWidth(220);
        leftList.setPrefHeight(700);
//        leftList.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT,null,null)));
//        leftList.getStyleClass().add("custom-jfx-list-view1");
        leftList.getStyleClass().addAll("custom-jfx-list-view1");
        leftList.depthProperty().setValue(1);
        leftList.expandedProperty().set(true);
        leftList.setVerticalGap(10.0);
        ObservableList<HBox> themeTypeObservableList = FXCollections.observableArrayList();
        themeTypeObservableList.add(getLeftContrlItem("图片组件",SVGLoadUtils.getInstance().getImageSVG(16,buttonColor)));
        leftList.setItems(themeTypeObservableList);
        leftList.getSelectionModel().selectFirst();
        return leftList;
    }

    public void showDialog(){
        stage.show();
    }


    private void initBtn(Button buttonCancel,Button buttonOk){
        buttonOk.setOnAction(event -> {
           Optional.ofNullable(imageControl.getCreateView()).ifPresent(baseView -> {
               baseViewConsumer.accept(baseView);
               Toast.makeText(new Stage(),"组件创建成功",1000);
               stage.close();
           });
        });
        buttonCancel.setOnAction(event -> {
                Optional.ofNullable(imageControl.getImagePath())
                        .ifPresent(path -> {
                            try {
                                Files.deleteIfExists(path);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        });
                stage.close();
        });
    }
}
